﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Configuration;
using System.Collections.Concurrent;
using System.Text;

namespace YLPublicService
{
    public abstract partial class ServiceBase<Tuser> : IServiceBase
        where Tuser : new()
    {
        protected static DbDataContext dbContext;


        protected FTPServerSetting.SettingManager settingManager;
        public static int[] FtpServerIPV4;
        public static int FtpServerPort;
        private readonly int randomPassLen = int.Parse(ConfigurationManager.AppSettings["RandomPassLength"]);

        public ServiceBase()
        {
            settingManager = new FTPServerSetting.SettingManager(ConfigurationManager.AppSettings["FTPConfigPath"]);
            FtpServerIPV4 = ConfigurationManager.AppSettings["FtpIp"].Split('.').Select(v => int.Parse(v)).ToArray();
            FtpServerPort = int.Parse(ConfigurationManager.AppSettings["FtpPort"]);
        }




        static ServiceBase()
        {
            dbContext = new DbDataContext();
            onlineUserByLoginName = new ConcurrentDictionary<string, Tuser>();
            onlineUserByGuid = new ConcurrentDictionary<Guid, Tuser>();
            onlineUserGuidByLoginName = new ConcurrentDictionary<string, Guid>();
        }




        public TransferModel.TemplateItem[] GetEnableTemplates(Guid guid, int enable)
        {
            if (!isOnline(guid))
            {
                return null;
            }
            var tnames = from t in dbContext.TemplatesV2s
                         where t.Enable == enable
                         select new TransferModel.TemplateItem
                         {
                             mEnable = enable.ToString(),
                             mID = t.TemplateID.ToString(),
                             mAttachmentsDir = t.AttachmentsDir,
                             mTemplateDetial = TransferModel.TemplateItem.TemplateDetial.FromXML(t.Detial)
                         }
                         ;
            return tnames.ToArray();
        }

        public TransferModel.TemplateItem[] GetEnableTemplates(Guid guid)
        {
            return GetEnableTemplates(guid, 1);
        }

        /// <summary>
        /// 测试使用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public int EchoInt(int value)
        {
            return value;
        }
        /// <summary>
        /// 测试使用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public string EchoString(string value)
        {
            return value;
        }
    }
}